package com.ruoyi.special.service.impl;

import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.db.domain.SpecialCostomInfo;
import com.ruoyi.db.domain.SpecialDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.SpecialCostomUserInfoMapper;
import com.ruoyi.db.domain.SpecialCostomUserInfo;
import com.ruoyi.special.service.ISpecialCostomUserInfoService;
import com.ruoyi.common.core.text.Convert;

/**
 * costomuserService业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-16
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class SpecialCostomUserInfoServiceImpl implements ISpecialCostomUserInfoService 
{
    @Autowired
    private SpecialCostomUserInfoMapper specialCostomUserInfoMapper;

    /**
     * 查询costomuser
     * 
     * @param id costomuser主键
     * @return costomuser
     */
    @Override
    public SpecialCostomUserInfo selectSpecialCostomUserInfoById(Long id)
    {
        return specialCostomUserInfoMapper.selectSpecialCostomUserInfoById(id);
    }

    /**
     * 查询costomuser列表
     * 
     * @param specialCostomUserInfo costomuser
     * @return costomuser
     */
    @Override
    public List<SpecialCostomUserInfo> selectSpecialCostomUserInfoList(SpecialCostomUserInfo specialCostomUserInfo)
    {
        return specialCostomUserInfoMapper.selectSpecialCostomUserInfoList(specialCostomUserInfo);
    }

    /**
     * 查询costomuser列表
     *
     * @param specialCostomUserInfo costomuser
     * @return costomuser
     */
    @Override
    public List<Map<String, Object>> selectSpecialCostomUserInfoListMap(SpecialCostomUserInfo specialCostomUserInfo)
    {
        return specialCostomUserInfoMapper.selectSpecialCostomUserInfoListMap(specialCostomUserInfo);
    }

    /**
     * 新增costomuser
     * 
     * @param specialCostomUserInfo costomuser
     * @return 结果
     */
    @Override
    public int insertSpecialCostomUserInfo(SpecialCostomUserInfo specialCostomUserInfo)
    {
        specialCostomUserInfo.setCreateTime(DateUtils.getNowDate());
        return specialCostomUserInfoMapper.insertSpecialCostomUserInfo(specialCostomUserInfo);
    }

    /**
     * 修改costomuser
     * 
     * @param specialCostomUserInfo costomuser
     * @return 结果
     */
    @Override
    public int updateSpecialCostomUserInfo(SpecialCostomUserInfo specialCostomUserInfo)
    {
        specialCostomUserInfo.setUpdateTime(DateUtils.getNowDate());
        return specialCostomUserInfoMapper.updateSpecialCostomUserInfo(specialCostomUserInfo);
    }

    /**
     * 批量删除costomuser
     * 
     * @param ids 需要删除的costomuser主键
     * @return 结果
     */
    @Override
    public int deleteSpecialCostomUserInfoByIds(String ids)
    {
        SpecialCostomUserInfo updateEntity = new SpecialCostomUserInfo();
        updateEntity.setId(Long.valueOf(ids));
        updateEntity.setStatus(0);
        updateEntity.setDelFlag(1);

        return specialCostomUserInfoMapper.updateSpecialCostomUserInfo(updateEntity);
    }

    /**
     * 删除costomuser信息
     * 
     * @param id costomuser主键
     * @return 结果
     */
    @Override
    public int deleteSpecialCostomUserInfoById(Long id)
    {
        return specialCostomUserInfoMapper.deleteSpecialCostomUserInfoById(id);
    }

    @Override
    public void insertUserList(List<SysUser> userList, SpecialCostomInfo costomInfo) {

        for (SysUser sysUser: userList){
            SpecialCostomUserInfo costomUserInfo = specialCostomUserInfoMapper.selectSpecialCostomUserInfoByAdminUserId(sysUser.getUserId());
            if (ObjectUtil.isNotEmpty(costomUserInfo)){
                continue;
            }
            costomUserInfo = new SpecialCostomUserInfo();
            costomUserInfo.setDeptId(sysUser.getDeptId());
            costomUserInfo.setCostomId(costomInfo.getId());
            costomUserInfo.setAdminUserId(sysUser.getUserId());
            costomUserInfo.setAdminUserName(sysUser.getUserName());
            costomUserInfo.setAdminUserPhone(sysUser.getLoginName());
            costomUserInfo.setCreateTime(DateUtils.getNowDate());
            specialCostomUserInfoMapper.insertSpecialCostomUserInfo(costomUserInfo);
        }
    }

    /**
     * 获取当前登陆用户对应的成员信息
     * @param sysUser
     * @return
     */
    @Override
    public SpecialCostomUserInfo getCurrentLoginUserInfo(SysUser sysUser)
    {
        SpecialCostomUserInfo costomUserInfo =
                specialCostomUserInfoMapper.selectSpecialCostomUserInfoByAdminUserId(sysUser.getUserId());
        return costomUserInfo;
    }

    @Override
    public Integer countOnlineUserNumber(Long infoId) {
        return specialCostomUserInfoMapper.countOnlineUserNumber(infoId);
    }

    @Override
    public List<SpecialCostomUserInfo> selectCostomUserInfoList(Long deptId) {
        return specialCostomUserInfoMapper.selectCostomUserInfoList(deptId);
    }
}
